package Week5;

import java.util.*;

public class Day26 {
}

//牛客:小红的ABC
class Main3 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        char[] s = in.next().toCharArray();
        int ret = -1;
        int n = s.length;
        for(int i = 0; i < n; i++){
            if(i + 1 < n && s[i] == s[i + 1]){
                ret = 2;
                break;
            }

            if(i + 2 < n && s[i] == s[i + 2]){
                ret = 3;
            }
        }
        System.out.println(ret);
    }
}

//牛客:DP23 不相邻取数
class Main4 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] arr = new int[n + 1];
        for(int i = 1; i <= n; i++){
            arr[i] = in.nextInt();
        }
        //创建dp表f,表示第i个位置的元素选但i-1个元素不选
        int[] f = new int[n + 1];
        //创建dp表g,表示第i个位置的元素不选
        int[] g = new int[n + 1];
        for(int i = 1; i <= n; i++){
            f[i] = g[i - 1] + arr[i];
            g[i] = Math.max(f[i - 1] , g[i - 1]);
        }
        System.out.println(Math.max(g[n] , f[n]));
    }
}


//牛客:空调遥控
//方法一:排序+滑动窗口
class Main5 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt() , p = in.nextInt();
        int[] arr = new int[n];
        for(int i = 0; i < n; i++){
            arr[i] = in.nextInt();
        }
        Arrays.sort(arr);
        int left = 0 , right = 0 , ret = 0;
        p *= 2;//最小值和最大值的长度差应为2*p
        while(right < n){
            //出窗口
            while(arr[right] - arr[left] > p){
                left++;
            }
            //判断长度的最大值
            ret = Math.max(ret , right - left + 1);
            //入窗口
            right++;
        }
        System.out.println(ret);
    }
}

//方法二:排序+二分
class Main6 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt() , p = in.nextInt();
        int[] arr = new int[n];
        for(int i = 0; i < n; i++){
            arr[i] = in.nextInt();
        }
        Arrays.sort(arr);
        int l = 0,r = 0 ,left = 0,right = n - 1,mid = 0;
        int ret = 0;
        for(int x : arr){
            left = 0;
            right = n - 1;
            while(left < right){
                //向上取整
                mid = (right + left) / 2 ;
                if(arr[mid] < x - p){
                    left = mid + 1;
                }else{
                    right = mid;
                }
            }
            l = left;
            left = 0;
            right = n - 1;
            while(left < right){
                //向下取整防止死循环超时
                mid = (right - left + 1) / 2 + left;
                if(arr[mid] > x + p){
                    right = mid - 1;
                }else{
                    left = mid;
                }
            }
            r = right;
            ret = Math.max(ret , r - l + 1);
        }
        System.out.println(ret);
    }
}